package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityAttachment;
import cc.comac.mom.bean.QualityAttachmentGroup;
import cc.comac.mom.bean.QuqlityQarCheckItem;
import cc.comac.mom.dao.entity.QuqlityQarCheckItemDao;
import cc.comac.mom.entity.QualityAttachmentEntity;
import cc.comac.mom.entity.QualityAttachmentGroupEntity;
import cc.comac.mom.entity.QuqlityQarCheckItemEntity;
import cc.comac.mom.redis.IdFactory;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
public class QuqlityQarCheckItemDataHandler  extends ServiceImpl<QuqlityQarCheckItemDao, QuqlityQarCheckItemEntity>{

	@Autowired
	private QuqlityQarCheckItemDao QuqlityQarCheckItemDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	@Autowired
	private QualityAttachmentGroupDataHandler qualityAttachmentGroupDataHandler;
	@Autowired
	private QualityAttachmentDataHandler qualityAttachmentDataHandler;
	
	@Autowired
	private IdFactory idFactory;
	//批量保存检验特性 ,前面调用id自动生成，这边不做生成
	
	public ResultBean insertBatch(List<QuqlityQarCheckItem> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null ||  list.size()<1) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		ArrayList<QuqlityQarCheckItemEntity> list2 = new ArrayList<QuqlityQarCheckItemEntity>();
		for(QuqlityQarCheckItem item :list) {
			if(item.getInnerData() == null) {
				resultBean.setErrorMessge("数据为空");
				return resultBean;
			}
			// 当存在attachmentId时，新增
			List<Long> attachmentIds = item.getInnerData().getAttachmentIds();
			if (attachmentIds != null && attachmentIds.size() > 0) {
				long attachmentGroupId = getAttachmentGroupId(attachmentIds, item.getInnerData().getCheckItem());
				item.getInnerData().setAttachmentGroupId(attachmentGroupId);
			}
			baseEntityDataUtil.setNotNullBaseEntityData(item.getInnerData());
			if (item.getInnerData() != null) {
				list2.add(item.getInnerData());
			}
			List<QuqlityQarCheckItem> listItemDetails = item.getListItemDetails();
			if (listItemDetails != null && listItemDetails.size() > 0 && listItemDetails.get(0) != null) {
				List<QuqlityQarCheckItemEntity> collect = listItemDetails.stream().map(QuqlityQarCheckItem::getInnerData).collect(Collectors.toList());
				for (QuqlityQarCheckItemEntity quqlityQarCheckItemEntity : collect) {
					// 当存在attachmentId时，新增
					if (quqlityQarCheckItemEntity != null) {
						baseEntityDataUtil.setNotNullBaseEntityData(quqlityQarCheckItemEntity);
						List<Long> ids = quqlityQarCheckItemEntity.getAttachmentIds();
						if (ids != null && ids.size() > 0) {
							long attachmentGroupId = getAttachmentGroupId(ids, item.getInnerData().getCheckItem());
							quqlityQarCheckItemEntity.setAttachmentGroupId(attachmentGroupId);
						}
					}
				}
				list2.addAll(collect);
			}
		}
		boolean saveBatch = this.saveBatch(list2);
		if(!saveBatch) {
			resultBean.setErrorMessge("批量保存QAR检验特性失败");
			return resultBean;
		}
		
		return resultBean;		
		
	}
	
	public List<QuqlityQarCheckItem> getByQarNo(String qarNo){
		QueryWrapper<QuqlityQarCheckItemEntity> queryWrapper = new QueryWrapper<QuqlityQarCheckItemEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("qar_no", qarNo);
		queryWrapper.eq("check_parent_id", 0);
		List<QuqlityQarCheckItemEntity> selectList = QuqlityQarCheckItemDao.selectList(queryWrapper);
		
		if(selectList == null || selectList.size()<1) {
			return null;
		}
		ArrayList<QuqlityQarCheckItem> listBean = new ArrayList<QuqlityQarCheckItem>();
		for(QuqlityQarCheckItemEntity item:selectList) {
			// 是否存在attachmentGroupId,存在赋值
			isExistAttachmentId(item);
			QuqlityQarCheckItem quqlityQarCheckItem = new QuqlityQarCheckItem();
			List<Long> attachIdByGroupId = qualityAttachmentGroupDataHandler.getAttachIdByGroupId(item.getGroupId());
			List<QualityAttachment> byAttachmentIds = qualityAttachmentDataHandler.getByAttachmentIds(attachIdByGroupId);
			if (byAttachmentIds != null && byAttachmentIds.size() > 0) {
				List<QualityAttachmentEntity> collect = byAttachmentIds.stream().map(QualityAttachment::getInnerData).collect(Collectors.toList());
//				item.setAttachmentIds(attachIdByGroupId);
//				item.setAttachmentList(collect);
			}
			quqlityQarCheckItem.setInnerData(item);
			// 当附属信息下面有子项数据时
			QueryWrapper<QuqlityQarCheckItemEntity> queryWrapper1 = new QueryWrapper<QuqlityQarCheckItemEntity>();
			queryWrapper1.eq("row_status", 1);
			queryWrapper1.eq("qar_no", qarNo);
			queryWrapper1.eq("check_parent_id", item.getId());
			List<QuqlityQarCheckItemEntity> listQuqlityQarCheckItemDetalsEntity = QuqlityQarCheckItemDao.selectList(queryWrapper1);
			if(listQuqlityQarCheckItemDetalsEntity != null && listQuqlityQarCheckItemDetalsEntity.size()>0) {
				ArrayList<QuqlityQarCheckItem> listItemDetails = new ArrayList<QuqlityQarCheckItem>();
				for(QuqlityQarCheckItemEntity item2:listQuqlityQarCheckItemDetalsEntity) {
					QuqlityQarCheckItem quqlityQarCheckItem2 = new QuqlityQarCheckItem();
					// 是否存在attachmentGroupId,存在赋值
					isExistAttachmentId(item2);
					quqlityQarCheckItem2.setInnerData(item2);
					List<Long> attachIdByGroupId1 = qualityAttachmentGroupDataHandler.getAttachIdByGroupId(item.getGroupId());
					item2.setAttachmentIds(attachIdByGroupId1);
					listItemDetails.add(quqlityQarCheckItem2);
				}
				quqlityQarCheckItem.setListItemDetails(listItemDetails);
				
			}
			listBean.add(quqlityQarCheckItem);
		}
		return listBean;
	}
	
	
	// 是否存在attachmentGroup存在获取这个对象
	public void isExistAttachmentId(QuqlityQarCheckItemEntity item) {
		if (item.getAttachmentGroupId()!= 0) {
			List<QualityAttachmentGroup> byAttachmentGroupId = qualityAttachmentGroupDataHandler.getByAttachmentGroupId(item.getAttachmentGroupId());
			item.setAttachmentGroupList(byAttachmentGroupId);
		}
		if (item.getGroupId() != 0) {
			List<QualityAttachmentGroup> byAttachmentGroupId = qualityAttachmentGroupDataHandler.getByAttachmentGroupId(item.getGroupId());
			item.setGroupList(byAttachmentGroupId);
		}
	}
	// 99999999
	public List<QuqlityQarCheckItem> getByQarNoList(List<String> qarNoList){
		QueryWrapper<QuqlityQarCheckItemEntity> queryWrapper = new QueryWrapper<QuqlityQarCheckItemEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.in("qar_no", qarNoList);
		queryWrapper.eq("check_parent_id", 0);
		List<QuqlityQarCheckItemEntity> selectList = QuqlityQarCheckItemDao.selectList(queryWrapper);
		if(selectList == null || selectList.size()<1) {
			return null;
		}
		ArrayList<QuqlityQarCheckItem> listBean = new ArrayList<QuqlityQarCheckItem>();
		for(QuqlityQarCheckItemEntity item:selectList) {
			// 是否存在attachmentGroupId,存在赋值
			isExistAttachmentId(item);
			QuqlityQarCheckItem quqlityQarCheckItem = new QuqlityQarCheckItem();
			quqlityQarCheckItem.setInnerData(item);
			// 当附属信息下面有子项数据时
			QueryWrapper<QuqlityQarCheckItemEntity> queryWrapper1 = new QueryWrapper<QuqlityQarCheckItemEntity>();
			queryWrapper1.eq("row_status", 1);
			queryWrapper1.eq("qar_no", item.getQarNo());
			queryWrapper1.eq("check_parent_id", item.getId());
			List<QuqlityQarCheckItemEntity> listQuqlityQarCheckItemDetalsEntity = QuqlityQarCheckItemDao.selectList(queryWrapper1);
			if(listQuqlityQarCheckItemDetalsEntity != null && listQuqlityQarCheckItemDetalsEntity.size()>0) {
				ArrayList<QuqlityQarCheckItem> listItemDetails = new ArrayList<QuqlityQarCheckItem>();
				for(QuqlityQarCheckItemEntity item2:listQuqlityQarCheckItemDetalsEntity) {
					// 是否存在attachmentGroupId,存在赋值
					isExistAttachmentId(item);
					QuqlityQarCheckItem quqlityQarCheckItem2 = new QuqlityQarCheckItem();
					quqlityQarCheckItem2.setInnerData(item2);
					listItemDetails.add(quqlityQarCheckItem2);
				}
				quqlityQarCheckItem.setListItemDetails(listItemDetails);
				
			}
			listBean.add(quqlityQarCheckItem);
		}
		return listBean;
	}
	
	// 通过AttachmentId保存数据并返回一个attachmentGroupId
	public long getAttachmentGroupId(List<Long> attachmentIds,String groupName) {
		List<QualityAttachment> byAttachmentIds = qualityAttachmentDataHandler.getByAttachmentIds(attachmentIds);
		long groupId = idFactory.generateId();
		if (byAttachmentIds.size() > 0) {
			List<QualityAttachmentGroup> qualityAttachmentGroupList = new ArrayList<QualityAttachmentGroup>();
			for (QualityAttachment qualityAttachment : byAttachmentIds) {
				QualityAttachmentGroup qualityAttachmentGroup = new QualityAttachmentGroup();
				QualityAttachmentGroupEntity qualityAttachmentGroupEntity = new QualityAttachmentGroupEntity();
				qualityAttachmentGroup.setInnerData(qualityAttachmentGroupEntity);
				qualityAttachmentGroupEntity.setAttachmentId(qualityAttachment.getInnerData().getId());
				qualityAttachmentGroupEntity.setGroupId(groupId);
				qualityAttachmentGroupEntity.setGroupName(groupName);
				baseEntityDataUtil.setBaseEntityData(qualityAttachmentGroupEntity);
				qualityAttachmentGroupList.add(qualityAttachmentGroup);
			}
			ResultBean insertBatch = qualityAttachmentGroupDataHandler.insertBatch(qualityAttachmentGroupList);
			if (!insertBatch.success) {
				throw new RuntimeException(insertBatch.getMessage());
			}
		}
		return byAttachmentIds.size() > 0 ?groupId : 0;
	}
	
	// 更新检验特性对应数据
	public void updateCheckItemData(List<QuqlityQarCheckItem> list,String qarNo) {
		if (list.size() > 0) {
			List<Long> ids = new ArrayList<Long>();
			for (QuqlityQarCheckItem quqlityQarCheckItem : list) {
				long id = quqlityQarCheckItem.getInnerData().getId();
				ids.add(id);
				long generateId = idFactory.generateId();
				quqlityQarCheckItem.getInnerData().setId(generateId);
				quqlityQarCheckItem.getInnerData().setQarNo(qarNo);
				QuqlityQarCheckItemEntity quqlityQarCheckItementity = quqlityQarCheckItem.getInnerData();
				if (quqlityQarCheckItementity.getAttachmentIds() != null && quqlityQarCheckItementity.getAttachmentIds().size() > 0) {
					List<Long> attachmentIds = quqlityQarCheckItementity.getAttachmentIds();
					if (attachmentIds != null && attachmentIds.size() > 0) {
						long attachmentGroupId = getAttachmentGroupId(attachmentIds, "QAR检验单特性上传");
						quqlityQarCheckItem.getInnerData().setGroupId(attachmentGroupId);
					}
				}
				List<QuqlityQarCheckItem> listItemDetails = quqlityQarCheckItem.getListItemDetails();
				if (listItemDetails != null && listItemDetails.size() > 0) {
					for (QuqlityQarCheckItem quqlityQarCheckItem1 : listItemDetails) {
						long id2 = quqlityQarCheckItem1.getInnerData().getId();
						ids.add(id2);
						long generateId2 = idFactory.generateId();
						quqlityQarCheckItem1.getInnerData().setQarNo(qarNo);
						quqlityQarCheckItem1.getInnerData().setId(generateId2);
						quqlityQarCheckItem1.getInnerData().setCheckParentId(generateId);
						QuqlityQarCheckItemEntity innerData = quqlityQarCheckItem1.getInnerData();
						if (innerData.getAttachmentIds() != null && innerData.getAttachmentIds().size() > 0) {
							long attachmentGroupId = getAttachmentGroupId(innerData.getAttachmentIds(), "QAR检验单特性上传");
							innerData.setAttachmentGroupId(attachmentGroupId);
						}
					}
				}
			}
			// 删除原有数据
			this.removeByIds(ids);
			ResultBean insertBatch = insertBatch(list);
			if (!insertBatch.success) {
				throw new RuntimeException(insertBatch.getMessage());
			}
		}
	}
	
}
